﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation
{

    public class PageSettings
    {
        public PageSettings()
        {
            kvTable.AddKeyValue("HorizontalCenter", horizontalCenter.ToString());
            kvTable.AddKeyValue("VerticalCenter", verticalCenter.ToString());
            kvTable.AddKeyValue("MarginLeft", marginLeft.ToString());
            kvTable.AddKeyValue("MarginRight", marginRight.ToString());
            kvTable.AddKeyValue("MarginTop", marginTop.ToString());
            kvTable.AddKeyValue("MarginBottom", marginBottom.ToString());
            kvTable.AddKeyValue("PaperName", paperName);
            kvTable.AddKeyValue("PaperDirection", paperDirection.ToString());

            //所有可读写的属性都在表中。
        }

        private KeyValueStringTable kvTable = new KeyValueStringTable();

        /// <summary>
        /// 将提供的信息转入kvTable列表中，以便取用。
        /// 并更新此类中各属性的值。
        /// </summary>
        /// <param name="pageSettingInfoString">
        /// 应提供形如：
        /// key1:value1|key2:value2|
        /// 的字符串。
        /// </param>
        public void SetPageSettings(string pageSettingInfoString)
        {
            kvTable.TableSource = pageSettingInfoString;

            //读出所有属性的值。

            try
            {
                horizontalCenter = bool.Parse(kvTable["HorizontalCenter"]);
                verticalCenter = bool.Parse(kvTable["VerticalCenter"]);
                marginLeft = double.Parse(kvTable["MarginLeft"]);
                marginRight = double.Parse(kvTable["MarginRight"]);
                marginTop = double.Parse(kvTable["MarginTop"]);
                marginBottom = double.Parse(kvTable["MarginBottom"]);

                paperName = kvTable["PaperName"];
                paperDirection = (Orientation)Enum.Parse(typeof(Orientation), kvTable["PaperDirection"]);
            }
            catch (Exception ex)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                "为页面设置字符串信息时意外出错！", "提供的值无法顺利转换。异常信息如下：\r\n" + ex.Message,
                ex.StackTrace);
                return;
            }
        }

        private bool horizontalCenter = false;

        public bool HorizontalCenter
        {
            get { return horizontalCenter; }
            set
            {
                horizontalCenter = value;
                kvTable["HorizontalCenter"] = value.ToString();
            }
        }

        private bool verticalCenter = false;

        public bool VerticalCenter
        {
            get { return verticalCenter; }
            set
            {
                verticalCenter = value;
                kvTable["VerticalCenter"] = value.ToString();
            }
        }

        private double marginLeft = 20.0 * PaperInfo.ratioValueFromMmToWpfPixcelUnits;

        public double MarginLeft
        {
            get { return marginLeft; }
            set
            {
                marginLeft = value;
                kvTable["MarginLeft"] = value.ToString();
            }
        }

        private double marginRight = 20.0 * PaperInfo.ratioValueFromMmToWpfPixcelUnits;

        public double MarginRight
        {
            get { return marginRight; }
            set
            {
                marginRight = value;
                kvTable["MarginRight"] = value.ToString();
            }
        }

        private double marginTop = 25.0 * PaperInfo.ratioValueFromMmToWpfPixcelUnits;

        public double MarginTop
        {
            get { return marginTop; }
            set
            {
                marginTop = value;
                kvTable["MarginTop"] = value.ToString();
            }
        }

        private double marginBottom = 25.0 * PaperInfo.ratioValueFromMmToWpfPixcelUnits;

        public double MarginBottom
        {
            get { return marginBottom; }
            set
            {
                marginBottom = value;
                kvTable["MarginBottom"] = value.ToString();
            }
        }

        private string paperName = "A4";

        public string PaperName
        {
            get { return paperName; }
            set
            {
                paperName = value;
                kvTable["PaperName"] = value.ToString();
            }
        }

        private Orientation paperDirection = Orientation.Vertical;

        public Orientation PaperDirection
        {
            get { return paperDirection; }
            set
            {
                paperDirection = value;
                kvTable["PaperDirection"] = value.ToString();
            }
        }

        /// <summary>
        /// 正常情况下会返回形如：
        /// key1:value1|key2:value2|的字符串。
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return kvTable.ToString();
        }

        //PaperInfo("A4", 793.7, 1122.5, 2480, 3507)
        public double PaperWidth
        {
            get
            {
                PaperInfo pi = PaperInfo.GetPaperInfo(this.paperName);

                if (pi != null)
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return pi.ShortSideWPFUnit;
                    }
                    else
                    {
                        return pi.LongSideWPFUnit;
                    }
                }
                else
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return 793.7;
                    }
                    else return 1122.5;
                }
            }
        }

        //PaperInfo("A4", 793.7, 1122.5, 2480, 3507)
        public double PaperHeight
        {
            get
            {
                PaperInfo pi = PaperInfo.GetPaperInfo(this.paperName);

                if (pi != null)
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return pi.LongSideWPFUnit;
                    }
                    else
                    {
                        return pi.ShortSideWPFUnit;
                    }
                }
                else
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return 1122.5;
                    }
                    else return 793.7;
                }
            }
        }

        //PaperInfo("A4", 793.7, 1122.5, 2480, 3507)
        public double PaperPixcelsX
        {
            get
            {
                PaperInfo pi = PaperInfo.GetPaperInfo(this.paperName);

                if (pi != null)
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return pi.ShortSidePixcelCount;
                    }
                    else
                    {
                        return pi.LongSidePixcelCount;
                    }
                }
                else
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return 793.7;
                    }
                    else return 1122.5;
                }
            }
        }

        //PaperInfo("A4", 793.7, 1122.5, 2480, 3507)
        public double PaperPixcelsY
        {
            get
            {
                PaperInfo pi = PaperInfo.GetPaperInfo(this.paperName);

                if (pi != null)
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return pi.LongSidePixcelCount;
                    }
                    else
                    {
                        return pi.ShortSidePixcelCount;
                    }
                }
                else
                {
                    if (this.paperDirection == Orientation.Vertical)
                    {
                        return 1122.5;
                    }
                    else return 793.7;
                }
            }
        }
    }
}
